package com.guoliang.eduService.controller;

import com.guoliang.commonUtils.CommonResult;
import com.guoliang.eduService.entity.EduChapter;
import com.guoliang.eduService.entity.vo.ChapterVo;
import com.guoliang.eduService.service.EduChapterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.index.PathBasedRedisIndexDefinition;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 课程 前端控制器
 * </p>
 *
 * @author leo
 * @since 2023-01-18
 */
@Api(description = "章节和小结")
@RestController
@RequestMapping("/eduService/eduChapter")
public class EduChapterController {
    @Autowired
    private EduChapterService eduChapterService;
    @ApiOperation("根据course_id获取章节和小节信息")
    @GetMapping("getChapter/{id}")
    public CommonResult getChapter(@PathVariable("id")String id){
        List<ChapterVo> chapterList=eduChapterService.getChapterAndVideo(id);
        return CommonResult.ok().data("chapters",chapterList);
    }
    @ApiOperation("根据id查询")
    @GetMapping("getChapterById/{id}")
    public CommonResult getChapterById(@PathVariable("id")String id){
        EduChapter chapter = eduChapterService.getById(id);
        return CommonResult.ok().data("chapter",chapter);
    }
    @ApiOperation("添加章节")
    @PostMapping("addChapter")
    public CommonResult addChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.save(eduChapter);
        return CommonResult.ok();
    }
    @ApiOperation("修改章节")
    @PutMapping("updateChapter")
    public CommonResult updateChapter(@RequestBody EduChapter eduChapter){
        eduChapterService.updateById(eduChapter);
        return CommonResult.ok();
    }
    @ApiOperation("删除")
    @DeleteMapping("deleteChapter/{id}")
    public CommonResult deleteChapter(@PathVariable("id") String id){
        boolean flag = eduChapterService.deleteChapter(id);
        return flag?CommonResult.ok():CommonResult.error();
    }
}
